package com.yozinga.fragment;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.yozinga.MerchantDetailActivity;
import com.yozinga.R;
import com.yozinga.adapter.CategaryAdapter;
import com.yozinga.database.yozingaDataBaseManager;
import com.yozinga.entity.Category;
import com.yozinga.entity.Offers;
import com.yozinga.util.SharedData;

public class FavouritesFragment extends ListFragment implements OnClickListener{
	ArrayList<Category> arrayList = null;
	public static ArrayList<Offers> offserList = null;
	HashMap<String,HashMap<String,Category>> totalCateryItemsHashMap;
	
	SharedData mSharedData;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_favorites,  container, false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mSharedData = new SharedData(getActivity());
		//For MerchantList Display ArrayList
		arrayList = new ArrayList<Category>();

		((ImageView)getView().findViewById(R.id.subDining)).setOnClickListener(this);
		((ImageView)getView().findViewById(R.id.subENT)).setOnClickListener(this);
		((ImageView)getView().findViewById(R.id.subServices)).setOnClickListener(this);
		((ImageView)getView().findViewById(R.id.subShopping)).setOnClickListener(this);
		((ImageView)getView().findViewById(R.id.subTravel)).setOnClickListener(this);
		((ImageView)getView().findViewById(R.id.subLoyality)).setOnClickListener(this);

		Cursor mCursor = getActivity().getContentResolver().query(yozingaDataBaseManager.Favourites.FAVOURITE_CONTENT_URI, null, null, null, null);
		Log.i("Today","Cursor Count is: "+mCursor.getCount());

		Category tempCategory;
		 totalCateryItemsHashMap = new HashMap<String,HashMap<String,Category>>();

		for(mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {

			tempCategory = new Category();
			String categoryId = mCursor.getString(mCursor.getColumnIndex(yozingaDataBaseManager.Favourites.COL_CATEGORYID));
			String merchantId = mCursor.getString(mCursor.getColumnIndex(yozingaDataBaseManager.Favourites.COL_MERCHANTID));
			String offerId = mCursor.getString(mCursor.getColumnIndex(yozingaDataBaseManager.Favourites.COL_OFFERID));

			tempCategory.setCategoryId(categoryId);
			tempCategory.setMerchantId(merchantId);
			tempCategory.setBusinessName(mCursor.getString(mCursor.getColumnIndex(yozingaDataBaseManager.Favourites.COL_MERCHANTNAME)));
			tempCategory.setMerchantTagLine(mCursor.getString(mCursor.getColumnIndex(yozingaDataBaseManager.Favourites.COL_MERCHANT_TAGLINE)));
			tempCategory.setLatitude(mCursor.getString(mCursor.getColumnIndex(yozingaDataBaseManager.Favourites.COL_MERCHANT_LATITUDE)));
			tempCategory.setLongitude(mCursor.getString(mCursor.getColumnIndex(yozingaDataBaseManager.Favourites.COL_MERCHANT_LONGITUDE)));

			tempCategory.setVideoUrl(mCursor.getString(mCursor.getColumnIndex(yozingaDataBaseManager.Favourites.COL_MERCHANT_VIDEO_LINK)));
			tempCategory.setMerchantFullAddress(mCursor.getString(mCursor.getColumnIndex(yozingaDataBaseManager.Favourites.COL_MERCHANT_FULL_ADDRESS)));
			tempCategory.setPhoneNumber(mCursor.getString(mCursor.getColumnIndex(yozingaDataBaseManager.Favourites.COL_MERCHANT_PHONE_NUMBER)));
			tempCategory.setInfo(mCursor.getString(mCursor.getColumnIndex(yozingaDataBaseManager.Favourites.COL_MERCHANT_INFO)));
			tempCategory.setFaceBookUrl(mCursor.getString(mCursor.getColumnIndex(yozingaDataBaseManager.Favourites.COL_MERCHANT_FACEBOOL_URL)));
			tempCategory.setTwitterUrl(mCursor.getString(mCursor.getColumnIndex(yozingaDataBaseManager.Favourites.COL_MERCHANT_TWITTER_URL)));

			if(!totalCateryItemsHashMap.containsKey(categoryId)){
				Log.i("Today","Category Is Created"+categoryId);
				totalCateryItemsHashMap.put(categoryId,new HashMap<String,Category>());
				HashMap<String,Category> tempMerchantItemsHashMap = totalCateryItemsHashMap.get(categoryId);
				if(!tempMerchantItemsHashMap.containsKey(merchantId)){
					Log.i("Today","Merchant Is Created");

					ArrayList<Offers> merchantOffersList = new ArrayList<Offers>();

					Offers offerDetails = new Offers();
					offerDetails.setOfferId(offerId);
					merchantOffersList.add(offerDetails);

					tempCategory.setOfferDetails(merchantOffersList);

					tempMerchantItemsHashMap.put(merchantId, tempCategory);

					totalCateryItemsHashMap.put(categoryId, tempMerchantItemsHashMap);

				}else{

					Log.i("Today","Merchant Is Updated");

					Category category = tempMerchantItemsHashMap.get(merchantId);

					ArrayList<Offers> merchantOffersList1 = category.getOfferDetails();

					Offers offerDetails = new Offers();
					offerDetails.setOfferId(offerId);
					merchantOffersList1.add(offerDetails);

					tempCategory.setOfferDetails(merchantOffersList1);

					tempMerchantItemsHashMap.put(merchantId, tempCategory);

					totalCateryItemsHashMap.put(categoryId, tempMerchantItemsHashMap);
				}
			}else{

				HashMap<String,Category> tempMerchantItemsHashMap = totalCateryItemsHashMap.get(categoryId);
				if(!tempMerchantItemsHashMap.containsKey(merchantId)){
					Log.i("Today","Merchant Is Created");

					ArrayList<Offers> merchantOffersList = new ArrayList<Offers>();

					Offers offerDetails = new Offers();
					offerDetails.setOfferId(offerId);
					merchantOffersList.add(offerDetails);

					tempCategory.setOfferDetails(merchantOffersList);
					tempMerchantItemsHashMap.put(merchantId, tempCategory);

					totalCateryItemsHashMap.put(categoryId, tempMerchantItemsHashMap);

				}else{

					Log.i("Today","Merchant Is Updated");

					Category category = tempMerchantItemsHashMap.get(merchantId);

					ArrayList<Offers> merchantOffersList1 = category.getOfferDetails();

					Offers offerDetails = new Offers();
					offerDetails.setOfferId(offerId);
					merchantOffersList1.add(offerDetails);

					tempCategory.setOfferDetails(merchantOffersList1);

					tempMerchantItemsHashMap.put(merchantId, tempCategory);

					totalCateryItemsHashMap.put(categoryId, tempMerchantItemsHashMap);
				}
			}
		}


		Log.i("Today","Categorys Size is: "+totalCateryItemsHashMap.size());
		Set<String> keySet = totalCateryItemsHashMap.keySet();
		Iterator<String> keySetIterator = keySet.iterator();
		while (keySetIterator.hasNext()) {
			String categoryId = keySetIterator.next();
			Log.i("Today","Retrive Category Id is:"+categoryId);
			if(totalCateryItemsHashMap.containsKey(categoryId)){
				HashMap<String,Category> hashMap = totalCateryItemsHashMap.get(categoryId);
				Log.i("Today","Merchants Size is: "+hashMap.size());
				Collection<Category> coll = hashMap.values();
				Iterator<Category> iter = coll.iterator();
				while (iter.hasNext())
				{
					Category category = iter.next();
					arrayList.add(category);
				}
			}
		}


		Log.i("Today","Total Merchant List in all Categories Size is: "+arrayList.size());

		ArrayList<String> mLogoList = new ArrayList<String>();
		mLogoList.add("http://www.google.co.in/imgres?imgurl=http://4.bp.blogspot.com/-8v_k_fOcfP8/UQIL4ufghBI/AAAAAAAAEDo/9ffRRTM9AnA/s400/android-robog-alone.png&imgrefurl=http://about-google-android.blogspot.com/2013/01/rumors-on-new-coming-expected-android.html&h=750&w=1160&sz=488&tbnid=RhMVr2gPztn0aM:&tbnh=89&tbnw=137&prev=/search%3Fq%3Dandroid%2Bimages%26tbm%3Disch%26tbo%3Du&zoom=1&q=android+images&usg=__zYdPt3VI4l996udAqLXqgsz7X2o=&docid=5FGZK-n-4XZ08M&sa=X&ei=9UjrUauXFM7HrQfHuIHIBA&sqi=2&ved=0CE4Q9QEwCw&dur=1510#imgdii=RhMVr2gPztn0aM%3A%3BZinz8J9QQm5J5M%3BRhMVr2gPztn0aM%3A");
		mLogoList.add("http://www.google.co.in/imgres?imgurl=http%3A%2F%2F4.bp.blogspot.com%2F-8v_k_fOcfP8%2FUQIL4ufghBI%2FAAAAAAAAEDo%2F9ffRRTM9AnA%2Fs400%2Fandroid-robog-alone.png&imgrefurl=http%3A%2F%2Fabout-google-android.blogspot.com%2F2013%2F01%2Frumors-on-new-coming-expected-android.html&docid=5FGZK-n-4XZ08M&tbnid=RhMVr2gPztn0aM%3A&w=1160&h=750&ei=FknrUYr1GcuHrgfCjYDADA&ved=0CAIQxiAwAA&iact=c");
		mLogoList.add("http://www.google.co.in/imgres?imgurl=http%3A%2F%2Fwww.androidblog.com.au%2Fwp-content%2Fuploads%2F2010%2F09%2FAndy-The-Android.jpg&imgrefurl=http%3A%2F%2Fwww.androidblog.com.au%2F2010%2F09%2Fandy-the-android-amigurumi-doll%2F&docid=_VO12_0Zt7EDIM&tbnid=1zblr6zqjYMS8M&w=1200&h=800&ei=FknrUYr1GcuHrgfCjYDADA&ved=0CAkQxiAwBw&iact=c");
		mLogoList.add("http://www.google.co.in/imgres?imgurl=http%3A%2F%2Fwww.androidblog.com.au%2Fwp-content%2Fuploads%2F2010%2F09%2FAndy-The-Android.jpg&imgrefurl=http%3A%2F%2Fwww.androidblog.com.au%2F2010%2F09%2Fandy-the-android-amigurumi-doll%2F&docid=_VO12_0Zt7EDIM&tbnid=1zblr6zqjYMS8M&w=1200&h=800&ei=FknrUYr1GcuHrgfCjYDADA&ved=0CAkQxiAwBw&iact=c");
		CategaryAdapter ca = new CategaryAdapter(arrayList,mLogoList,getActivity());
		setListAdapter(ca);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		Intent merchantIntent = new Intent(getActivity(),MerchantDetailActivity.class);

		Category category = (Category) arrayList.get(position);

		//Merchant ID for Offer Details
		merchantIntent.putExtra("merchantName", category.getBusinessName());
		merchantIntent.putExtra("merchantId",category.getMerchantId());

		//MerchantInfo information
		merchantIntent.putExtra("merchantVideoLink",category.getVideoUrl());
		merchantIntent.putExtra("merchantFullAddress",category.getMerchantFullAddress());
		merchantIntent.putExtra("merchantPhoneNumber",category.getPhoneNumber());
		merchantIntent.putExtra("merchantInfo","");
		merchantIntent.putExtra("merchantFacebook",category.getFaceBookUrl());
		merchantIntent.putExtra("merchantTwitter",category.getTwitterUrl());
		//merchantIntent.putExtra("merchantPaypol",category.getPaypalEmail());
		merchantIntent.putExtra("categoryId",category.getCategoryId());
		merchantIntent.putExtra("merchantTagLine", category.getMerchantTagLine());

		//MerchantInfo Map
		merchantIntent.putExtra("latitude",category.getLatitude());
		merchantIntent.putExtra("longitude",category.getLongitude());
		merchantIntent.putExtra("ScreenName","Favourites");
		//merchantIntent.putExtra("offersList", category.getOfferDetails());
		offserList = category.getOfferDetails();
		startActivity(merchantIntent);
	}

	@Override
	public void onClick(View v) {
		HashMap<String,Category> hashMap = null;
		//For MerchantList Display ArrayList
		arrayList = new ArrayList<Category>();
		
		if(v.getId() == R.id.subDining){
			
			if(totalCateryItemsHashMap.get(mSharedData.getBusinessId("Restaurant"))!=null)
			hashMap = totalCateryItemsHashMap.get(mSharedData.getBusinessId("Restaurant"));
			
		}else
		if(v.getId() == R.id.subENT){
			
			if(totalCateryItemsHashMap.get(mSharedData.getBusinessId("Entertainment"))!=null)
			hashMap = totalCateryItemsHashMap.get(mSharedData.getBusinessId("Entertainment"));
			
		}else
		if(v.getId() == R.id.subServices){
			
			if(totalCateryItemsHashMap.get(mSharedData.getBusinessId("Services"))!=null)
			hashMap = totalCateryItemsHashMap.get(mSharedData.getBusinessId("Services"));
			
		}else
		if(v.getId() == R.id.subTravel){
			
			if(totalCateryItemsHashMap.get(mSharedData.getBusinessId("TH"))!=null)
			hashMap = totalCateryItemsHashMap.get(mSharedData.getBusinessId("TH"));
			
		}else
		if(v.getId() == R.id.subLoyality){
			
			if(totalCateryItemsHashMap.get(mSharedData.getBusinessId("Loyalty"))!=null)
			hashMap = totalCateryItemsHashMap.get(mSharedData.getBusinessId("Loyalty"));
			
		}else
		if(v.getId() == R.id.subShopping){
			
			if(totalCateryItemsHashMap.get(mSharedData.getBusinessId("Shopping"))!=null)
			hashMap = totalCateryItemsHashMap.get(mSharedData.getBusinessId("Shopping"));
			
		}
		
		//Log.i("Today","Merchants Size is: "+hashMap.size());
		ArrayList<String> mLogoList = new ArrayList<String>();
		mLogoList.add("http://www.google.co.in/imgres?imgurl=http://4.bp.blogspot.com/-8v_k_fOcfP8/UQIL4ufghBI/AAAAAAAAEDo/9ffRRTM9AnA/s400/android-robog-alone.png&imgrefurl=http://about-google-android.blogspot.com/2013/01/rumors-on-new-coming-expected-android.html&h=750&w=1160&sz=488&tbnid=RhMVr2gPztn0aM:&tbnh=89&tbnw=137&prev=/search%3Fq%3Dandroid%2Bimages%26tbm%3Disch%26tbo%3Du&zoom=1&q=android+images&usg=__zYdPt3VI4l996udAqLXqgsz7X2o=&docid=5FGZK-n-4XZ08M&sa=X&ei=9UjrUauXFM7HrQfHuIHIBA&sqi=2&ved=0CE4Q9QEwCw&dur=1510#imgdii=RhMVr2gPztn0aM%3A%3BZinz8J9QQm5J5M%3BRhMVr2gPztn0aM%3A");
		mLogoList.add("http://www.google.co.in/imgres?imgurl=http%3A%2F%2F4.bp.blogspot.com%2F-8v_k_fOcfP8%2FUQIL4ufghBI%2FAAAAAAAAEDo%2F9ffRRTM9AnA%2Fs400%2Fandroid-robog-alone.png&imgrefurl=http%3A%2F%2Fabout-google-android.blogspot.com%2F2013%2F01%2Frumors-on-new-coming-expected-android.html&docid=5FGZK-n-4XZ08M&tbnid=RhMVr2gPztn0aM%3A&w=1160&h=750&ei=FknrUYr1GcuHrgfCjYDADA&ved=0CAIQxiAwAA&iact=c");
		mLogoList.add("http://www.google.co.in/imgres?imgurl=http%3A%2F%2Fwww.androidblog.com.au%2Fwp-content%2Fuploads%2F2010%2F09%2FAndy-The-Android.jpg&imgrefurl=http%3A%2F%2Fwww.androidblog.com.au%2F2010%2F09%2Fandy-the-android-amigurumi-doll%2F&docid=_VO12_0Zt7EDIM&tbnid=1zblr6zqjYMS8M&w=1200&h=800&ei=FknrUYr1GcuHrgfCjYDADA&ved=0CAkQxiAwBw&iact=c");
		mLogoList.add("http://www.google.co.in/imgres?imgurl=http%3A%2F%2Fwww.androidblog.com.au%2Fwp-content%2Fuploads%2F2010%2F09%2FAndy-The-Android.jpg&imgrefurl=http%3A%2F%2Fwww.androidblog.com.au%2F2010%2F09%2Fandy-the-android-amigurumi-doll%2F&docid=_VO12_0Zt7EDIM&tbnid=1zblr6zqjYMS8M&w=1200&h=800&ei=FknrUYr1GcuHrgfCjYDADA&ved=0CAkQxiAwBw&iact=c");
		
		if(hashMap!=null){
			Collection<Category> coll = hashMap.values();
			Iterator<Category> iter = coll.iterator();
			while (iter.hasNext()){
				Category category = iter.next();
				arrayList.add(category);
			}
		}
		Log.i("Today","Total Merchant List in all Categories Size is: "+arrayList.size());
		CategaryAdapter ca = new CategaryAdapter(arrayList,mLogoList,getActivity());
		setListAdapter(ca);
	}
}
